<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/7
 * Time: 下午4:05
 */

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\Room;
use App\Http\Model\RoomTurntable;
use App\Http\Model\RoomMicro;

class RoomTurntableServices extends CommonServices
{
    private $modelRoomTurntable;
    private $modelRoom;
    private $modelRoomMicro;

    public function __construct()
    {
        $this->modelRoomTurntable = new RoomTurntable();
        $this->modelRoom = new Room();
        $this->modelRoomMicro = new RoomMicro();
        parent::__construct();
    }


    /**
     * 转盘开启状态 0 - 关闭 1 - 开启
     * @return array
     */
    public function state()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('room_id', '房间ID', 'int', true, '房间ID不能为空');
            $this->I('state', 'state', 'int', true, 'state不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            //extract($variables); //数组参数化
            $roomId = $this->requestData['room_id'];
            $state = $this->requestData['state'] ?? 0;
            $curUser = $this->user->get_current_user();
            if ($this->modelRoom->get_role($roomId, $curUser['user_id']) == 0) {
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            $this->modelRoomTurntable->setTurntableState($roomId, $state);
            $data['state'] = 1;
        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 刷新转盘
     * @return array
     */
    public function refresh()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('room_id', '房间ID', 'int', true, '房间ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            //extract($variables); //数组参数化
            $roomId = $this->requestData['room_id'];
            $curUser = $this->user->get_current_user();
            if ($this->modelRoom->get_role($roomId, $curUser['user_id']) == 0) {
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 开启转盘
     * @return array
     */
    public function publish()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('room_id', '房间ID', 'int', true, '房间ID不能为空');
            $this->I('type', 'type', 'int', true, '转盘类型');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            //extract($variables); //数组参数化
            $roomId = $this->requestData['room_id'];
            $type = $this->requestData['type'];
            $curUser = $this->user->get_current_user();
            if ($this->modelRoom->get_role($roomId, $curUser['user_id']) == 0) {
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            if ($this->modelRoomTurntable->getTurntableOpened($roomId) != 1) {
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            $data['state'] = 1;
            $publishData = $this->getPublishData($roomId);
            $data['user'] = $publishData['user'];

            if ($type == 1) {
                $data['select'] = $publishData['userids'] ? [array_random($publishData['userids'])] : [];
            }

            if ($type == 2) {
                $data['select'] = $publishData['userids'] ? $publishData['userids'] : [];
            }

            if (empty($data['user']) || empty($data['select'])) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
        } while (false);

        return compact('code', 'msg', 'data');
    }


    /**
     * 获取当前麦用户
     * @param $roomId
     * @return array user 用户列表 userids 用户ids
     */
    public function getPublishData($roomId){
        $room = $this->modelRoom->get_room($roomId);
        $typeId = $room['room_type']['id'];

        $select_data = $this->modelRoomMicro->get_micros($roomId, $typeId);
        $userIdArr = [];

        foreach ($select_data as $v){
            if ($v['user_id']) {
                $userIdArr[] = $v['user_id'];
            }
        }

        if ($userIdArr) {
            $userArr = $this->user->get_users_by_ids($userIdArr);
            $userData = array_column($userArr, null, 'user_id');
            $userList = [];
            foreach ($select_data as $k => $v){
                $user = $userData[$v['user_id']] ?? null;
                if (!empty($user)){
                    $userList[$k]['nickname'] = $user['nickname'];
                    $userList[$k]['gender'] = $user['gender'];
                    $userList[$k]['user_id'] = $user['user_id'];
                    $userList[$k]['thumbnails'] = ($user['thumbnails'] && isset($user['thumbnails'][0])) ? $user['thumbnails'][0] : "";
                }
            }
            $userList = array_values($userList);

            shuffle($userIdArr);

            return ["user" => $userList, "userids" => $userIdArr];
        }

        return ["user" => [], "userids" => []];
    }


}